﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class EnrolQuit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            TxtAmount.Attributes["onkeyup"] = "value=value.replace(/[^\\d]/g,'')";
            txtUnitPrise.Attributes["onkeyup"] = "value=value.replace(/[^\\d]/g,'')";
            SqlConnection conn = DB.GetCon();
            conn.Open();
            this.AutoID(conn);//自动编号
            this.bind();
            this.Panel1.Visible = false;
            DataSet ds = DB.reDs("SELECT  Title FROM tb_BigType where Type='物资大类'");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                //显示数据库中的类别并给TreeView根节点
                TreeNode tree1 = new TreeNode(ds.Tables[0].Rows[i][0].ToString());
                this.TreeView1.Nodes.Add(tree1);
                //tree1.Expand();
                DataSet ds1 = DB.reDs("select title from tb_Type Where type='" + ds.Tables[0].Rows[i][0].ToString() + "'");
                //显示TreeView根节点下的子节点
                for (int j = 0; j < ds1.Tables[0].Rows.Count; j++)
                {
                    TreeNode tree2 = new TreeNode(ds1.Tables[0].Rows[j][0].ToString(), ds1.Tables[0].Rows[j][0].ToString());
                    tree1.ChildNodes.Add(tree2);
                }
            }
        }
    }
    //根据数据库中的记录数自动产生一个数作为入库单ID
    public void AutoID(SqlConnection conn)
    {
        SqlCommand com = new SqlCommand("select count(*) from tb_Quit", conn);
        int Count = Convert.ToInt32(com.ExecuteScalar());
        this.txtQuitID.Text = Convert.ToString(Count + 1);
        if (conn.State == ConnectionState.Open)
        {
            com.Dispose();
            conn.Close();
        }
        this.labStorage.Text = "0";
    }
    public void bind()
    {
        DataSet ds1 = DB.reDs("select Title from tb_BigType where Type='仓库'");
        this.ddlStorePlace.DataSource = ds1;
        ddlStorePlace.DataTextField = "Title";
        ddlStorePlace.DataBind();
        DataSet ds2 = DB.reDs("select * from tb_BigType where type='出库类型'");
        this.ddlType.DataSource = ds2;
        ddlType.DataTextField = "Title";
        this.ddlType.DataBind();
        DataSet ds3 = DB.reDs("select Title from tb_BigType where type='部门'");
        this.ddlDepartment.DataSource = ds3;
        ddlDepartment.DataTextField = "Title";
        ddlDepartment.DataBind();
        DataSet ds4 = DB.reDs("select * from tb_Enterprise  where type1='是'");
        this.ddlCustomer.DataSource = ds4;
        ddlCustomer.DataTextField = "Name";
        ddlCustomer.DataValueField = "CustomerID";
        ddlCustomer.DataBind();
        DataSet ds5 = DB.reDs("select * from tb_BigType  where type='计量单位'");
        this.ddlUnit.DataSource = ds5;
        ddlUnit.DataTextField = "Title";
        ddlUnit.DataBind();
    }
    //使页面恢复初始状态
    public void clear()
    {
        foreach (Control c in this.form1.Controls)//遍历控件查找文本框控件，并清空
        {
            if (c.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
            {
                TextBox t = (TextBox)c;
                t.Text = "";
            }
        }
        foreach (Control c in this.form1.Controls)//遍历控件查找DropDownList控件，并恢复默认选项
        {
            if (c.GetType().ToString() == "System.Web.UI.WebControls.DropDownList")
            {
                DropDownList d = (DropDownList)c;
                d.SelectedIndex = -1;
            }
        }
    }
    protected void btnClickOK_Click(object sender, EventArgs e)
    {
        string QuitID = this.txtQuitID.Text.Trim();
        string QuitDate = this.txtQuitDate.Text.Trim();
        string QuitType = this.ddlType.SelectedValue.ToString();
        string ddlStorePlace = this.ddlStorePlace.SelectedValue.ToString();
        string OutMan = this.txtOutMan.Text.Trim();
        string txtQuitMan = this.txtQuitMan.Text.Trim();
        string CustomerID = this.ddlCustomer.SelectedValue.ToString();
        string Customer = this.ddlCustomer.SelectedItem.Text.ToString();
        string Address = this.txtAddress.Text.Trim();
        string LinkMan = this.txtLinkMan.Text.Trim();
        string Phont = this.txtPhone.Text.Trim();
        string QuitMoney = this.txtQuitMoney.Text.Trim();
        string QuitDepartment = this.ddlDepartment.SelectedValue.ToString();
        string Remark = this.txtRemark.Text.Trim();
        string WzID = this.lnkWzID.Text;
        string Name = this.txtName.Text;
        string Modle = this.txtModel.Text;
        string Spec = this.txtSpec.Text;
        string Card = this.txtCard.Text;
        string Unit = this.ddlUnit.SelectedValue.ToString();
        string UnitPrice = this.txtUnitPrise.Text.Trim();
        string Amount = this.TxtAmount.Text.Trim();
        string  Gathering = this.txtGathering.Text.Trim();
        try
        {
            bool b=DB.ExSql(" insert into tb_Quit values(' " + QuitID + "','" + QuitDate + "','" + QuitType + "','" + ddlStorePlace + "','" + OutMan + "','" + CustomerID + "','" + Customer + "','" + Address + "','" + Phont + "','" + LinkMan + "'," + QuitMoney + ",'" + QuitDepartment + "','" + txtQuitMan + "','" + Remark + "')");
            if(b)
                DB.ExSql("insert into tb_QuitDetails values('" + QuitID + "','" + WzID + "','" + Name + "','" + Modle + "','" + Spec + "','" + Card + "','" + Unit + "'," + UnitPrice + ",'" + Amount + "'," + Gathering + ")");
            //使页面恢复初始状态
            this.clear();
            SqlConnection conn = DB.GetCon();
            conn.Open();
            AutoID(conn);//根据数据库中的记录数自动产生一个数作为入库单ID
            this.lnkWzID.Text = "物资编号";
        }
        catch (SqlException oe)
        {
           Response.Write(oe.Message.ToString());
        }
    }
    protected void ddlCustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ddlCustomer.SelectedItem.ToString() != "选择供应商")
        {
            string CustomerID = this.ddlCustomer.SelectedValue.ToString();
            DataSet ds = DB.reDs("select * from tb_Enterprise  where CustomerID='" + CustomerID + "'");
            this.txtAddress.Text = ds.Tables[0].Rows[0][2].ToString();
            this.txtPhone.Text = ds.Tables[0].Rows[0][3].ToString();
            this.txtLinkMan.Text = ds.Tables[0].Rows[0][4].ToString();
        }
        else
        {
            this.txtAddress.Text = "";
            this.txtPhone.Text = "";
            this.txtLinkMan.Text = "";
        }
    }
    protected void gvStatistic_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        this.Panel1.Visible = false;
        string id = this.gvStatistic.DataKeys[e.NewSelectedIndex].Value.ToString();
        DataSet ds = DB.reDs("select * from tb_Material where WzID='" + id + "'");
        this.lnkWzID.Text = ds.Tables[0].Rows[0][0].ToString();
        this.txtName.Text = ds.Tables[0].Rows[0][2].ToString();
        this.txtModel.Text = ds.Tables[0].Rows[0][3].ToString();
        this.txtSpec.Text = ds.Tables[0].Rows[0][4].ToString();
        this.txtCard.Text = ds.Tables[0].Rows[0][5].ToString();
        this.txtUnitPrise.Text = ds.Tables[0].Rows[0][6].ToString();

        //计算库存
        DataTable dtStore = DB.ExcuteDataTable("SELECT SUM(Amount) FROM tb_StoreDetails WHERE (WzID = " + id + ")");
        int store = 0;
        if (dtStore.Rows[0][0].ToString() != "")
        {
            store = Convert.ToInt32(dtStore.Rows[0][0].ToString());
        }
        DataTable dtStoreOut = DB.ExcuteDataTable("SELECT SUM(Amount)FROM tb_OutDetails WHERE (WzID = " + id + ")");
        DataTable dtQuit = DB.ExcuteDataTable("SELECT SUM(Amount)FROM tb_QuitDetails WHERE (WzID = " + id + ")");
        int storeOut = 0;
        int quit = 0;
        if (dtStoreOut.Rows[0][0].ToString() != "")
        {
            storeOut = Convert.ToInt32(dtStoreOut.Rows[0][0].ToString());
        }
        if (dtQuit.Rows[0][0].ToString() != "")
        {
            quit = Convert.ToInt32(dtQuit.Rows[0][0].ToString());
        }
        this.labStorage.Text = Convert.ToString(store - storeOut - quit);
    }
    protected void lnkButtonWzID_Click(object sender, EventArgs e)
    {
        this.Panel1.Visible = true;
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        DataSet ds = DB.reDs("select * from tb_Material where WzType='" + this.TreeView1.SelectedNode.Value.ToString() + "'");
        this.gvStatistic.DataSource = ds;
        this.gvStatistic.DataKeyNames = new string[] { "WzID" };
        this.gvStatistic.DataBind();
    }
    protected void TxtAmount_TextChanged(object sender, EventArgs e)
    {
        if (Convert.ToInt32(this.TxtAmount.Text) > Convert.ToInt32(this.labStorage.Text))
        {
            Response.Write("<script>alert('库存不足！');</script>");
            this.TxtAmount.Focus();
        }
        else
        {
            float UnitPrise = float.Parse(this.txtUnitPrise.Text.ToString());
            int Amount = int.Parse(this.TxtAmount.Text.ToString());
            this.txtGathering.Text = Convert.ToString(UnitPrise * Amount);
            this.txtQuitMoney.Text = Convert.ToString(UnitPrise * Amount);
        }
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string time = args.Value;
        try
        {
            DateTime datetime = Convert.ToDateTime(time);
        }
        catch
        {
            CustomValidator1.Text = "您输入的时间格式有误";
            args.IsValid = false;
        }
    }
}
